package com.bawei.module_mine.ui.login

import android.os.Bundle
import android.view.MotionEvent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.flow_eventbus.LoginEvent
import com.bawei.lib_common.flow_eventbus.post.postEvent
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.module_mine.R
import com.bawei.module_mine.databinding.ActivityLoginBinding
import com.bawei.module_mine.ui.login.view.PhoneLoginFragment
import com.bawei.myclub.base.mvvm.view.BaseToolbarActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 *@Author：lxy
 *@Description:
 *@Time：2025/5/28
 */
@AndroidEntryPoint
@Route(path = Router.Ui.LoginActivity)
class LoginActivity @Inject constructor() :
    BaseToolbarActivity<LoginViewModel, ActivityLoginBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_login

    override val mViewModel: LoginViewModel by viewModels()

    private lateinit var mNavController: NavController //导航控制器

    companion object {
        fun jump() {
            ARouter.getInstance().build(Router.Ui.LoginActivity)
                .withTransition(
                    com.bawei.lib_common.R.anim.bottom_in,
                    com.bawei.lib_common.R.anim.fab_out
                ).navigation()
        }
    }

    override fun initView() {
        super.initView()
        mNavController =
            (supportFragmentManager.findFragmentById(R.id.nav_host_container) as NavHostFragment).navController
    }

    override fun initListener() {
        //密码登陆
        mToolbar.setOnMenuItemClickListener {
            if (it.itemId == R.id.action) {
                mNavController.navigate(R.id.action_phoneLoginFragment_to_accountLoginFragment)
            }
            true
        }

        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                mViewModel.currentFragment.collect {
                    manageFragment(it)
                }
            }
        }


    }

    override fun initData() {

    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev!!.action == MotionEvent.ACTION_DOWN) {
            // postEvent(this, ClickAreaEvent(ev))
            return super.dispatchTouchEvent(ev)
        }

        return if (window.superDispatchTouchEvent(ev)) {
            true
        } else super.dispatchTouchEvent(ev)
    }

    private fun manageFragment(it: String) {
        title = ""
        mToolbar.menu.clear()
        when (it) {
            PhoneLoginFragment::class.simpleName -> {
                mToolbar.setNavigationIcon(R.mipmap.icon_exit)
                mToolbar.inflateMenu(R.menu.single_menu)
                mToolbar.menu.getItem(0).title = "密码登陆"
                mToolbar.menu.getItem(0).icon = null
            }

            else -> {
                mToolbar.setNavigationIcon(androidx.appcompat.R.drawable.abc_ic_ab_back_material)
            }

        }
    }

    fun handleLoginSuccess() {
        //发送登录成功消息
        postEvent(LoginEvent())
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()

    }

    class ClickAreaEvent(val ev: MotionEvent)

}